import gcoord from "../../miniprogram_npm/gcoord/dist/gcoord";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    selectedObj: {}, // 筛选条件
    pageNum: 1,
    longitude: '',
    latitude: '',
    originList: [],
    total: 0,
    type: '',
    sortList: [],
    activeIndex: 0,
    sortColumn: '',
    sortRule: '',
    showSelect: false,
    nameType: '1',
    labelList: []
  },


  // 排序
  goSort() {
    this.setData({
      showSelect: true
    })
  },
  onClose() {
    this.setData({
      showSelect: false,
    });
  },
  selectOne(e) {
    const index = e.currentTarget.dataset.index
    const sortColumn = e.currentTarget.dataset.sortcolumn
    const sortRule = e.currentTarget.dataset.sortrule
    this.setData({
      activeIndex: index,
      sortColumn: sortColumn,
      sortRule: sortRule,
      showSelect: false,
    })
    this.clickCy();
  },
  clickCy(e) {
    // 切换后重置分页参数和列表原始数组
    this.setData({
      pageNum: 1,
      originList: [],
    });
    this.getInfos();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const nlist =  wx.getStorageSync('labelList')
    let labelList = []
    nlist.forEach(pre => {
      labelList.push(`${pre.name}：${pre.value}`)
    })
    const selectedObj = JSON.parse(options.selectedObj)
    let list = []
    if (options.type == '1') {
      list = [{
          sortColumn: 'creditScore',
          sortRule: 'desc',
          label: '评分从高到低'
        },
        {
          sortColumn: 'creditScore',
          sortRule: 'asc',
          label: '评分从低到高'
        },
        {
          sortColumn: 'esdate',
          sortRule: 'asc',
          label: '成立日期由远及近'
        },
        {
          sortColumn: 'esdate',
          sortRule: 'desc',
          label: '成立日期由近及远'
        },
      ];
      this.setData({
        sortColumn: 'creditScore',
        sortRule: 'desc',
        nameType: options.nameType || '1'
      })
    } else {
      list = [{
          sortColumn: 'distance',
          sortRule: 'asc',
          label: '距离由近及远'
        },
        {
          sortColumn: 'distance',
          sortRule: 'desc',
          label: '距离由远及近'
        },
        {
          sortColumn: 'creditScore',
          sortRule: 'desc',
          label: '评分从高到低'
        },
        {
          sortColumn: 'creditScore',
          sortRule: 'asc',
          label: '评分从低到高'
        },
        {
          sortColumn: 'esdate',
          sortRule: 'asc',
          label: '成立日期由远及近'
        },
        {
          sortColumn: 'esdate',
          sortRule: 'desc',
          label: '成立日期由近及远'
        },
      ];
      this.setData({
        sortColumn: 'distance',
        sortRule: 'asc',
      })
    }
    this.setData({
      selectedObj: selectedObj,
      type: options.type,
      sortList: list,
      labelList: labelList
    })
    const that = this;
    // 获取当前的位置信息
    getApp().globalData.getLocation({
      type: "gcj02",
      success(res) {
        that.setData({
          longitude: that.transMarker([res.longitude, res.latitude])[0],
          latitude: that.transMarker([res.latitude, res.latitude])[1],
        });
        // 获取当前的定位
        that.getInfos()
      },
    });
  },
  goBack() {
    wx.navigateBack({
      delta: -1
    })
  },

  async getInfos() {
    wx.showLoading({
      title: "加载中",
    });
    let params = {

    }
    console.log(this.data.type, 'this.data.type');
    if (this.data.type == 1) {
      // 全量的条件
      params = {
        pageBO: {
          pageSize: 25,
          pageNum: this.data.pageNum,
        },
        sortColumn: this.data.sortColumn,
        sortRule: this.data.sortRule,
      }
    } else {
      // 附近的条件
      params = {
        userLng: this.data.longitude,
        userLat: this.data.latitude,
        // userLng: 120.681605,
        // userLat: 31.322302,
        distance: '2.0',
        sortColumn: this.data.sortColumn,
        sortRule: this.data.sortRule,
        pageBO: {
          pageSize: 25,
          pageNum: this.data.pageNum,
        },
      }
    }
    console.log({
      ...params,
      ...this.data.selectedObj
    }, 'params企业列表查询参数');
    const {
      list,
      total
    } = await getApp().globalData.request(
      "/map/lookAroundV2", {
        ...params,
        ...this.data.selectedObj
      },
      "POST"
    );
    wx.hideLoading();
    console.log(list, 'list')
    this.setData({
      originList: this.data.originList.concat(list),
      total: total,
      loading: false
    });
  },

  toprodetail(e) {
    wx.navigateTo({
      url: `/pages/mapDetail/mapDetail?corpKey=${e.detail.corpKey}&latitude=${e.detail.latitude}&longitude=${e.detail.longitude}&distance=${''}`,
    });
  },

  // 坐标系转化
  transMarker(item) {
    const resae = gcoord.transform(
      [item[0], item[1]],
      gcoord.GCJ02, // 当前坐标系
      gcoord.BD09 // 目标坐标系
    );
    return resae;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      pageNum: ++this.data.pageNum
    })
    this.getInfos()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});